package im.starchat.chat.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;
import im.starchat.chat.R;
import im.starchat.chat.activity.StarActivity;
import im.starchat.chat.impl.IFollowingCallback;
import im.starchat.chat.rest.model.ChannelBean;
import im.starchat.chat.utils.PreferencesStorageUtil;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by liuchao on 2/10/16.
 * 频道适配器
 */
public class ChannelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private static final int TYPE_FEED = 1001;
    private static final int TYPE_EMPTY = 1002;
    private List<ChannelBean> mList;

    private IFollowingCallback mFollowingCallback;

    public ChannelAdapter(Context context, IFollowingCallback iFollowingCallback) {
        this.mContext = context;
        this.mFollowingCallback = iFollowingCallback;
    }

    public void refreshData(List<ChannelBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void addAll(List<ChannelBean> list){
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    public void changeAttentionState(int position, boolean state){
        mList.get(position).setIs_following(state);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_EMPTY:
                return new EmptyHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.view_empty, parent, false));
            case TYPE_FEED:
            default:
                return new ParadeHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_channel, parent, false));

        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)) {
            case TYPE_EMPTY:
                EmptyHolder emptyHolder = (EmptyHolder) holder;
                emptyHolder.mInfoText.setText("暂无新明星");
                break;
            case TYPE_FEED:
                final ParadeHolder paradeHolder = (ParadeHolder) holder;

                if (!TextUtils.isEmpty(mList.get(position).getAvatar())) {
                    Picasso.with(mContext).load(mList.get(position).getAvatar())
                            .placeholder(R.drawable.home_bg)
                            .into(paradeHolder.imageButtonUserLogo);
                }
                paradeHolder.imageButtonUserLogo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!PreferencesStorageUtil.getUserId(mContext).equals(mList.get(position).getId())) {
                            Intent viewUserInfointent = new Intent();
                            viewUserInfointent.setClass(mContext, StarActivity.class);
                            viewUserInfointent.putExtra(StarActivity.USER_ID, mList.get(position).getId() + "");
                            mContext.startActivity(viewUserInfointent);
                        }
                    }
                });
                paradeHolder.textViewDescribe.setText(mList.get(position).getDescription());
                paradeHolder.textViewUserName.setText(mList.get(position).getNickname());
                if(mList.get(position).is_following()){
                    paradeHolder.mCancelAttentionBtn.setVisibility(View.VISIBLE);
                    paradeHolder.mAttentionBtn.setVisibility(View.GONE);
                }else{
                    paradeHolder.mCancelAttentionBtn.setVisibility(View.GONE);
                    paradeHolder.mAttentionBtn.setVisibility(View.VISIBLE);
                }
                paradeHolder.mAttentionBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(mFollowingCallback != null){
                            mFollowingCallback.onFollowing(String.valueOf(mList.get(position).getId()), position);
                        }
                    }
                });
                paradeHolder.mCancelAttentionBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(mFollowingCallback != null){
                            mFollowingCallback.onCancelFollowing(String.valueOf(mList.get(position).getId()), position);
                        }
                    }
                });

                if (mList != null && mList.get(position).getImages().length > 0 && !TextUtils.isEmpty(mList.get(position).getImages()[0])) {
                    Picasso.with(mContext).load(mList.get(position).getImages()[0])
                            .placeholder(R.drawable.channel_bg)
                            .into(paradeHolder.mImage1);
//                    paradeHolder.mImage1.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            Intent viewImg1Intent = new Intent();
//                            viewImg1Intent.setClass(mContext, ImageViewerActivity.class);
//                            viewImg1Intent.putExtra(ImageViewerActivity.IMAGE_VIEW_URL_TRANSFER, mList.get(position).getImages()[0]);
//                            mContext.startActivity(viewImg1Intent);
//                        }
//                    });
                }else{
                    paradeHolder.mImage1.setImageResource(R.drawable.channel_bg);
                }
                if (mList.get(position).getImages() != null && mList.get(position).getImages().length > 1 && !TextUtils.isEmpty(mList.get(position).getImages()[1])) {
                    Picasso.with(mContext).load(mList.get(position).getImages()[1])
                            .placeholder(R.drawable.channel_bg)
                            .into(paradeHolder.mImage2);
                }else{
                    paradeHolder.mImage2.setImageResource(R.drawable.channel_bg);
                }
                if (mList.get(position).getImages() != null && mList.get(position).getImages().length > 2 && !TextUtils.isEmpty(mList.get(position).getImages()[2])) {
                    Picasso.with(mContext).load(mList.get(position).getImages()[2])
                            .placeholder(R.drawable.channel_bg)
                            .into(paradeHolder.mImage3);
                }else{
                    paradeHolder.mImage3.setImageResource(R.drawable.channel_bg);
                }
                if (mList.get(position).getImages() != null && mList.get(position).getImages().length > 3 && !TextUtils.isEmpty(mList.get(position).getImages()[3])) {
                    Picasso.with(mContext).load(mList.get(position).getImages()[3])
                            .placeholder(R.drawable.channel_bg)
                            .resize(300,300)
                            .into(paradeHolder.mImage4);
                }else{
                    paradeHolder.mImage4.setImageResource(R.drawable.channel_bg);
                }

                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(mList == null || mList.size() == 0){
            return TYPE_EMPTY;
        }else{
            return TYPE_FEED;
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null|| mList.size() == 0) {
            return 1;
        }
        return mList.size();
    }



    public class ParadeHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.image_btn_parade_user_logo)
        public RoundedImageView imageButtonUserLogo;
        @InjectView(R.id.user_name)
        public TextView textViewUserName;
        @InjectView(R.id.describe)
        public TextView textViewDescribe;
        @InjectView(R.id.attention)
        public TextView mAttentionBtn;
        @InjectView(R.id.cancel_attention)
        public TextView mCancelAttentionBtn;
        @InjectView(R.id.image1)
        public ImageView mImage1;
        @InjectView(R.id.image2)
        public ImageView mImage2;
        @InjectView(R.id.image3)
        public ImageView mImage3;
        @InjectView(R.id.image4)
        public ImageView mImage4;

        public ParadeHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class EmptyHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.info_text)
        TextView mInfoText;

        public EmptyHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }


}
